package com.blinddate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blinddate.context.BaseContext;
import com.blinddate.mapper.InterestMapper;
import com.blinddate.mapper.UserInfMapper;
import com.blinddate.pojo.entity.Interest;
import com.blinddate.pojo.entity.UserInf;
import com.blinddate.pojo.vo.UserInfVO;
import com.blinddate.service.InterestService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class InterestServiceImpl implements InterestService {
    @Autowired
    private InterestMapper interestMapper;
    @Autowired
    private UserInfMapper userInfMapper;

    /**
     * 展示用户列表
     * @param page
     * @param size
     * @return
     */
    public List<UserInfVO> list(Integer page, Integer size) {
        IPage iPage=new Page(page,size);
        LambdaQueryWrapper<Interest> lqw =new LambdaQueryWrapper<Interest>();
        lqw.eq(Interest::getUserId,BaseContext.getCurrentId());
        interestMapper.selectPage(iPage,lqw);
        List<Interest> interests=iPage.getRecords();
        List<UserInfVO> userInfList=new ArrayList<>();
        for (Interest interest : interests) {
            LambdaQueryWrapper<UserInf> lq =new LambdaQueryWrapper<UserInf>();
            lq.eq(UserInf::getUserId,interest.getGetId());
            UserInf userInf=userInfMapper.selectOne(lq);
            UserInfVO userInfVO=new UserInfVO();
            userInfVO.setUserInf(userInf);
            userInfList.add(userInfVO);
        }
        return userInfList;
    }

    /**
     * 收藏用户
     * @param id
     */
    public void add(Long id) {
        Interest interest=new Interest();
        interest.setUserId(BaseContext.getCurrentId());
        interest.setGetId(id);
        interestMapper.insert(interest);
    }

    /**
     * 取消收藏用户
     * @param id
     */
    public void delete(Long id) {
        LambdaQueryWrapper<Interest> lqw=new LambdaQueryWrapper<Interest>();
        lqw.eq(Interest::getGetId,id).eq(Interest::getUserId,BaseContext.getCurrentId());
        interestMapper.delete(lqw);
    }
}
